const $jm = {
    excelSave(data, token, callback) {
        const saveFn = (obj) => {
            //判断报表是否有分组字段
            const rows = data.rows;
            let groupField = "";
            for (let key in rows) {
                if (groupField) break;
                const cells = rows[key].cells;
                for (let cellKey in cells) {
                    const text = cells[cellKey].text;
                    if (!text) continue;
                    if (text.includes(".group(")) {
                        groupField = text.replace("group(", "")
                            .replace(")}", "")
                            .replace("#{", "")
                        break;

                    }
                }
            }
            if (groupField) {
                data.isGroup = true;
                data.groupField = groupField;
            }
            if (data.chartList && data.chartList.length > 0) {
                data.chartList.forEach(item => {
                    if (item.width === 0 || item.height === 0) {
                        item.width = "650";
                        item.height = "350";
                    }
                })
            }
            let st = '', et = ''
            if (obj.reportType.indexOf('日') > -1) {
                let t = getDateTime('day', obj.time)
                st = t
                et = t
            } else if (obj.reportType.indexOf('月') > -1) {
                let t = getDateTime('month', obj.time)
                st = t
                et = t
            } else if (obj.reportType.indexOf('年') > -1) {
                let t = getDateTime('year', obj.time)
                st = t
                et = t
            } else if (obj.reportType.indexOf('自定义') > -1) {
                st = getDateTime('', obj.time[0])
                et = getDateTime('', obj.time[1])
            } else {
                st = obj.time
                et = obj.time
            }
            let dataStr = JSON.stringify({
                designerObj: obj, ...data,
                params: {
                    reportType: obj?.reportType,
                    dataGrarity: obj.dataGrarity,
                    startTime: st,
                    endTime: et
                }
            });
            if (obj.name != null && obj.name != "") {
                $http.post({
                    url: api.saveReport,
                    data: dataStr,
                    contentType: 'json',
                    success: (result) => {
                        onbeforeunload = "return true";
                        // 判断是否有回调函数
                        if (callback) {
                            callback(result);
                        }
                    }
                })
            }
        }
        vm.handleCreate(saveFn, data);
    },
    excelGet(code, callback) {
        if (code != null && code != "") {
            $http.get({
                url: api.getReport(code),
                success: (result) => {
                    Vue.prototype.$Spin.hide();
                    if (result) {
                        callback && callback(result);
                    }
                }
            })
        }
    },
    excelView(id, requestParam, callback, fail, error, url) {
        let apiUrl = url ? url : '';
        $http.get({
            url: api.show,
            data: { "id": id, "apiUrl": apiUrl, "params": JSON.stringify(requestParam) },
            success: (result) => {
                callback(result);
            }, fail: (result) => {
                fail && fail(result);
            }, error: (result) => {
                error && error(result)
            }
        })
    },
    addViewCount(id) {
        $http.post({
            contentType: 'json',
            url: api.addViewCount(id)
        })
    },
    checkParam(id, callback, fail) {
        $http.get({
            url: api.checkParam(id),
            success: (result) => {
                callback(result);
            },
            fail: (result) => {
                fail && fail(result)
            }
        })
    },
    //update-begin---author:wangshuai ---date:20220427  for：[issues/I5310Z]图形刷新和搜索条件的bug------------
    async qurestSql(requestParam, callback) {
        await $http.post({
            url: api.qurestSql,
            contentType: 'json',
            data: requestParam,
            //update-end---author:wangshuai ---date:20220427  for：[issues/I5310Z]图形刷新和搜索条件的bug------------
            success: (result) => {
                callback(result);
            }
        })
    },
    //update-begin---author:wangshuai ---date:20220427  for：[issues/I5310Z]图形刷新和搜索条件的bug------------
    async qurestApi(requestParam, callback) {
        await $http.post({
            url: api.qurestApi,
            contentType: 'json',
            data: requestParam,
            //update-end---author:wangshuai ---date:20220427  for：[issues/I5310Z]图形刷新和搜索条件的bug------------
            success: (result) => {
                callback(result);
            }
        })
    },
    qureyByApiUrl(url, callback) {
        $http.get({
            url: url,
            success: (result) => {
                callback(result);
            }, fail: (res) => {
                callback(res);
            }
        })
    },
    //校验唯一性
    async dataCodeExist(id, code, callback) {
        await $http.get({
            url: api.dataCodeExist,
            data: { "reportId": id, "code": code },
            success: (result) => {
                callback(result);
            }
        })
    },
    //获取查询信息
    getQueryInfo(id, param, token, callback) {
        let params = { "reportId": id, "param": JSON.stringify(param) }
        $http.get({
            url: api.getQueryInfo,
            data: params,
            success: (result) => {
                callback(result);
            }
        })
    },
    //下载图片
    downLoadImage(domain, url) {
        return $http.metaGet(`${domain}/report/download/image?imageUrl=${url}`)
    },
    //获取数据 报表内部使用
    getAllData(excelConfigId, paramString) {
        return $http.metaGet(`${api.show}?id=${excelConfigId}&params=${paramString}`)
    }

};